package MemberController;

import java.io.IOException;


import java.util.*;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class MemberJoinAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String sessionID  = null;
	private HttpSession session = null;
   
	public MemberJoinAction() {
        super();
        
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		joinProcess(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		joinProcess(request, response);
	}
	
	protected void joinProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("*****회원가입모듈이동*****");
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String numMode = request.getParameter("num");
		String action_code = request.getParameter("action_code");
		
		RequestDispatcher view = null;
		PrintWriter out =response.getWriter();
		
		MembersBean mbean = new MembersBean();
		MembersDAO  mdao  = new MembersDAO();
		
		session = request.getSession();
		
		int start = numMode != null ? Integer.parseInt(numMode) : -1;
		
		if(action_code.equals("join")){
			switch(start){
			case 0: 
					System.out.println("*****회원메인페이지 이동*****");
					view =request.getRequestDispatcher("/Main/FirstMain/Fm_Join_main.jsp");
					view.forward(request,response);
					break;
					
			
			case 1:
					System.out.println("*****회원가입페이지 이동*****");
					request.setAttribute("pagename","Fm_JoinWrite");
					request.setAttribute("upjoin","1");
					view =request.getRequestDispatcher("/Main/FirstMain/Fm_Join1.jsp");
					view.forward(request,response);
					break;
			case 2:
					System.out.println("*****회원가입확인페이지이동*****");
					
					mbean.setMem_Nick(request.getParameter("Mem_Nick"));
					mbean.setMem_Pass(request.getParameter("Mem_Pass"));
					mbean.setMem_Question(request.getParameter("Mem_Question"));
					mbean.setMem_Answer(request.getParameter("Mem_Answer"));
					mbean.setMem_Level(Integer.parseInt(request.getParameter("Mem_Level")));
					mbean.setMem_NameK(request.getParameter("Mem_NameK"));
					mbean.setMem_NameH(request.getParameter("Mem_NameH"));
					mbean.setMem_Fpost(request.getParameter("Mem_Fpost"));
					mbean.setMem_Spost(request.getParameter("Mem_Spost"));
					mbean.setMem_Address1(request.getParameter("Mem_Address1"));
					mbean.setMem_Address2(request.getParameter("Mem_Address2"));
					mbean.setMem_Phone(request.getParameter("Mem_Phone"));
					mbean.setMem_Mobile(request.getParameter("Mem_Mobile"));
					mbean.setMem_Fax(request.getParameter("Mem_Fax"));
					mbean.setMem_Email(request.getParameter("Mem_Email"));
					mbean.setMem_Year(request.getParameter("Mem_Year"));
					mbean.setMem_Month(request.getParameter("Mem_Month"));
					mbean.setMem_Day(request.getParameter("Mem_Day"));
					
					ArrayList joinList = new ArrayList();
					joinList.add(mbean);
					request.setAttribute("pagename","Fm_JoinCheck");
					request.setAttribute("upjoin","2");
					session.setAttribute("joinList",joinList);
					view = request.getRequestDispatcher("/Main/FirstMain/Fm_Join1.jsp");
					view.forward(request, response);
					break;
			
			case 3:
				System.out.println("*****회원가입완료*****");
				
				System.out.println();
				mbean.setMem_Nick(request.getParameter("Mem_Nick"));
				mbean.setMem_Pass(request.getParameter("Mem_Pass"));
				mbean.setMem_Question(request.getParameter("Mem_Question"));
				System.out.println(request.getParameter("Mem_Question"));
				mbean.setMem_Answer(request.getParameter("Mem_Answer"));
				mbean.setMem_Level(Integer.parseInt(request.getParameter("Mem_Level")));
				mbean.setMem_NameK(request.getParameter("Mem_NameK"));
				mbean.setMem_NameH(request.getParameter("Mem_NameH"));
				mbean.setMem_Fpost(request.getParameter("Mem_Fpost"));
				mbean.setMem_Spost(request.getParameter("Mem_Spost"));
				mbean.setMem_Address1(request.getParameter("Mem_Address1"));
				mbean.setMem_Address2(request.getParameter("Mem_Address2"));
				mbean.setMem_Phone(request.getParameter("Mem_Phone"));
				mbean.setMem_Mobile( request.getParameter("Mem_Mobile"));
				mbean.setMem_Fax(request.getParameter("Mem_Fax"));
				mbean.setMem_Email(request.getParameter("Mem_Email"));
				mbean.setMem_Year(request.getParameter("Mem_Year"));
				mbean.setMem_Month(request.getParameter("Mem_Month"));
				mbean.setMem_Day(request.getParameter("Mem_Day"));
				
				mdao.mjoin(mbean);
				
				request.setAttribute("pagename","Fm_JoinAcept");
				request.setAttribute("upjoin","3");
				view = request.getRequestDispatcher("/Main/FirstMain/Fm_Join1.jsp");
				view.forward(request, response);
				break;
			
			case 4:
				System.out.println("*****회원가입페이지 이동*****");
				request.setAttribute("pagename","Fm_JoinWrite_Old");
				request.setAttribute("upjoin","1");
				view =request.getRequestDispatcher("/Main/FirstMain/Fm_Join1.jsp");
				view.forward(request,response);
				break;
			}
		}
	}
}

